package the.broccoli.setting.interfaces.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.broccoli.setting.dto.LogCondition;
import the.broccoli.setting.service.ILogService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 日志控制器
 *
 * @author lei.liu
 * @since 2020-04-18 14:06:22
 */
@RestController
@RequestMapping("/setting/log")
public class LogController {

    @Resource
    private ILogService logService;

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        logService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }


    @GetMapping("/{logId}")
    public APIResult get(@PathVariable(value = "logId") String logId) {
        return APIResult.success(logService.getById(logId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult> page(
            @ApiParam(name = "logType", value = "日志类型", required = false)
            @RequestParam(name = "logType", required = false) String logType,

            @ApiParam(name = "operateTimeQs", value = "操作起始时间", required = false)
            @RequestParam(name = "operateTimeQs", required = false) Date operateTimeQs,

            @ApiParam(name = "operateTimeJs", value = "操作结束时间", required = false)
            @RequestParam(name = "operateTimeJs", required = false) Date operateTimeJs,

            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        LogCondition condition = new LogCondition();
        condition.setTenantId(UserContext.tenantId());
        condition.setLogType(logType);
        condition.setOperateTimeQs(operateTimeQs);
        condition.setOperateTimeJs(operateTimeJs);
        condition.setKeyword(keyword);
        condition.setOrderBy(orderBy);
        return APIResult.success(logService.pageList(condition, pageNum, pageSize));
    }
}